package reduce.fz.com.reduce.custom.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;


import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.util.PicSizeUtil;
import reduce.fz.com.reduce.util.http.ImageLoader;


/**
 * 圆角ImageView
 *
 * @author lipf
 *
 */
public class RoundNetworkImageView extends NetworkImageView {

    public RoundNetworkImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a;
        a = context.obtainStyledAttributes( attrs, R.styleable.RoundNetworkImageView);
        radius = a.getDimension(R.styleable.RoundNetworkImageView_radius, 6)/2;
        a.recycle();
        init();
    }

    public RoundNetworkImageView(Context context) {
        super(context);
        init();
    }

    /**
     * 头像都是用这个方法
     * @param url The URL that should be loaded into this ImageView.
     * @param imageLoader ImageLoader that will be used to make the request.
     */
    public void setAvatarImageUrl(String url, ImageLoader imageLoader) {
        url = PicSizeUtil.getAvatarUrl(url);
        super.setImageUrl(url, imageLoader);
    }

    /**
     * 非头像且从远程加载的圆角图片都是用此方法
     * @param url
     * @param imageLoader
     */
    public void setImageUrlOriginal(String url, ImageLoader imageLoader) {
        super.setImageUrl(url, imageLoader);
    }

    /**
     * 设置头像
     * @param url
     * @param imageLoader
     */
    public void setImageUrl(String url,ImageLoader imageLoader)
    {
        super.setImageUrl(url,imageLoader);
    }



    private final RectF roundRect = new RectF();
    private float radius = 16;
    private final Paint maskPaint = new Paint();
    private final Paint zonePaint = new Paint();

    private void init() {
        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //
        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);
        //
        float density = getResources().getDisplayMetrics().density;
        radius = radius * density;
    }

    public void setRadius(float radius) {
        this.radius = radius;
        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int w = getWidth();
        int h = getHeight();
        roundRect.set(0, 0, w, h);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
        canvas.drawRoundRect(roundRect, radius, radius, zonePaint);
        //
        canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
        super.draw(canvas);
        canvas.restore();
    }

}
